package client;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;

import communication.ClientComm;

public class ConsoleController implements Runnable {
	private ClientModel client;
	private ClientComm clientComm;
	private BufferedReader kbScan;
	private InputInterpreter interpreter;
	private Thread controller;
	private Queue<String> inputs;
	private boolean exiting;
	
	public ConsoleController(ClientModel cl, ClientComm comm){
		kbScan = new BufferedReader(new InputStreamReader(System.in));
		client = cl;
		clientComm = comm;
		exiting = false;
		inputs = new LinkedList<String>();
		interpreter = new InputInterpreter(this, client, clientComm);
		controller = new Thread(this, "clientKeybReader");
		controller.start();
	}
	
	@Override
	public void run() {
		String input;
		try {
			while ( !exiting && (input = kbScan.readLine()) != null) {
				inputs.offer(input);
				interpreter.sveglia();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void close() {
		exiting = true;
	}
	
	public String peekInput() {
		return inputs.peek();
	}
	
	public String pollInput() {
		return inputs.poll();
	}
	
	public void setView(View v){
		interpreter.setView(v);
	}
}
